﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Exhibition.Dao.ViewModel;
using AutoMapper;
namespace Exhibition.Dao
{

    public class AttentionDao
    {


        public AttentionDao()
        {

            Mapper.CreateMap<E_Attention, AttentionModel>();
        }
        public List<E_Attention> GetList(int startIndex, int maxRow, out int recordcount)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                recordcount = edm.E_Attention.Count();

                return edm.E_Attention.OrderByDescending(s => s.Id).Skip(startIndex).Take(maxRow).ToList();

            }

        }


        public AttentionModel GetOneById(int id)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                var query = edm.E_Attention.FirstOrDefault(s => s.Id == id);
                return Mapper.Map<E_Attention, AttentionModel>(query);
            }

        }

        public int GetLikeUser(int id,int siteId,int cutp)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {
                if (cutp == 1)
                {
                     return edm.E_Attention.Where(s => s.UserId == id && s.SiteId == siteId).Count();
                }
                else {
                    return edm.E_Attention.Where(s => s.ReUserId == id && s.SiteId == siteId).Count();
                }
            }

        }



        public int Save(AttentionModel obj)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                var query = edm.E_Attention.FirstOrDefault(s => s.Id == obj.Id);

                if (query == null)
                {


                    query = new E_Attention();

                    edm.AddToE_Attention(query);

                }


                query.UserId = obj.UserId;

                query.ReUserId = obj.ReUserId;

                query.CreateTime = obj.CreateTime;

                edm.SaveChanges();

                return query.Id;


            }

        }


        public int DelById(int id)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                var query = edm.E_Attention.FirstOrDefault(s => s.Id == id);

                if (query != null)
                {


                    edm.DeleteObject(query);


                }


                return edm.SaveChanges();


            }

        }


    }

}

